package com.enjoy.game.xcard.sprite;

import org.andengine.entity.Entity;
import org.andengine.entity.IEntity;
import org.andengine.entity.modifier.IEntityModifier;
import org.andengine.entity.modifier.IEntityModifier.IEntityModifierListener;
import org.andengine.entity.scene.ITouchArea;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.shape.RectangularShape;
import org.andengine.entity.sprite.AnimatedSprite;
import org.andengine.entity.sprite.Sprite;
import org.andengine.entity.sprite.batch.SpriteBatch;
import org.andengine.entity.sprite.batch.vbo.ISpriteBatchVertexBufferObject;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.shader.ShaderProgram;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.region.ITiledTextureRegion;
import org.andengine.opengl.vbo.DrawType;
import org.andengine.opengl.vbo.IVertexBufferObject;
import org.andengine.opengl.vbo.VertexBufferObjectManager;
import org.andengine.util.algorithm.collision.RectangularShapeCollisionChecker;
import org.andengine.util.modifier.IModifier;

import com.enjoy.game.xcard.action.MovementController;

public class CellLayout extends Entity implements IExEntity, ITouchArea{
	int mColumnNum, mRowNum;
	float mCellWidth, mCellHeight;
	XCard[] mEntityArray;
	MovementController mController;
	CellFullListner mCellFullListner;
	int mPressedIndex = -1;
	
	
	public CellLayout(float x, float y, float cellWidth, float cellHeight, int columnNum, int rowNum) {
		super(x, y);
		mCellWidth = cellWidth;
		mCellHeight = cellHeight;
		mColumnNum = columnNum;
		mRowNum =  rowNum;
		mEntityArray = new XCard[columnNum * rowNum];
	}

	public boolean acceptCard(){
		return findEmptyCell() >= 0;
	}
	
	public boolean acceptCard(float localX , float localY){
		return getCellIndex(localX, localY) >= 0;
	}
	
	public  int findEmptyCell(){
		for(int i = 0; i < mEntityArray.length; i ++){
			if(mEntityArray[i] == null){
				return i;
			}
		}
		return -1;
	}
	
	public  int findEntity(IEntity entity){
		for(int i = 0; i < mEntityArray.length; i ++){
			if(mEntityArray[i] == entity){
				return i;
			}
		}
		return -1;
	}
	
	
	public boolean putCard(XCard card){
		return putCard(card, -1);
	}
	
	public boolean putCard(XCard card, float localX, float localY){
		int index = getCellIndex(localX, localY);
		if(index < 0){
			return false;
		}
		
		return putCard(card, index);
		
	}
	
	public boolean putCard(XCard card, int index) {
		if(index == -1){
			index = findEmptyCell();
		}
		if(index >= 0 && index < mEntityArray.length && mEntityArray[index] == null){
			mEntityArray[index] = card;
			int cellX = index%mColumnNum;
			int cellY = index/mColumnNum;

			attachChild(card);
			card.setPosition(cellX*mCellWidth, cellY*mCellHeight);
			
			if(findEmptyCell() < 0 && mCellFullListner != null){
				mCellFullListner.onCellFull(this);
			}
			return true;
		}
		
		if(mCellFullListner != null){
			mCellFullListner.onCellFull(this);
		}
		return false;
		
	}
	
	public boolean removeCard(XCard card){
		int index = findEntity(card);
		if(index >= 0){
			detachChild(card);
			mEntityArray[index] = null;
			return true;
		}
		return false;
	}


	@Override
	public void onAttachScene(Scene scene) {
		//ExEntityUtil.onAttachScene(scene, this);
		scene.registerTouchArea(this);
	}

	@Override
	public boolean canAcceptDrag() {
		return ExEntityUtil.canAcceptDrag(this);
	}

	@Override
	public boolean dispatchDragEvent(EntityDragEvent event) {
		return ExEntityUtil.dispatchDragEvent(event, this);
	}


	@Override
	public boolean contains(float pX, float pY) {
		return RectangularShapeCollisionChecker.checkContains(this, mCellWidth*mColumnNum,  mCellHeight*mRowNum, pX, pY);
	}

	public int getCellIndex(float localX, float localY){
		int column  = (int)(localX/mCellWidth);
		int  row = (int)(localY/mCellHeight);
		if(row < mRowNum && column < mColumnNum){
			return  row * mColumnNum + column;
		}
		return -1;
	}
	
	@Override
	public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
			float pTouchAreaLocalX, float pTouchAreaLocalY) {
		switch (pSceneTouchEvent.getAction()) {
		case TouchEvent.ACTION_DOWN:
			int pressedIndex = getCellIndex(pTouchAreaLocalX, pTouchAreaLocalY);
			if(pressedIndex >= 0 && pressedIndex < mEntityArray.length && mEntityArray[pressedIndex] != null){
				mPressedIndex = pressedIndex;
				XCard card = mEntityArray[mPressedIndex];
				if(card != null && mController != null){
					mController.triger(card, this);
				}
			}
			break;
		case TouchEvent.ACTION_MOVE:
			break;
			
		case TouchEvent.ACTION_UP:
			if (mPressedIndex >= 0 && mPressedIndex == getCellIndex(pTouchAreaLocalX, pTouchAreaLocalY)) {
				//TODO:
			}
			break;
		}
		return true;
	}
	
	public void setMovementController(MovementController movementController){
		mController = movementController;
	}
	
	public void setFullListner(CellFullListner l){
		mCellFullListner = l;
	}
	
	public interface CellFullListner{
		void onCellFull(CellLayout layout);
	}

}
